package com.example.wx.Controller;


import com.example.wx.Dao.MasterDao;
import com.example.wx.Dao.UserDao;

import com.example.wx.Entity.Master;
import com.example.wx.Entity.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class webController {

    @Autowired
    UserDao userDao;

    @Autowired
    MasterDao masterDao;

    @RequestMapping("/")
    public String getHtml222() {
        return "login";
    }

    @RequestMapping("/index")
    public String getHtml() {
        return "index";
    }

    @RequestMapping("/search")
    public String getHtml1112() {
        return "search";
    }

    @RequestMapping("/userindex")
    public String getHtml11() {
        return "userindex";
    }

    @RequestMapping("/inandout")
    public String getHtml1111() {
        return "inandout";
    }

    @RequestMapping("/health")
    public String getHtml11111() {
        return "health";
    }

    @RequestMapping("/power")
    public String getHtml2(String id) {
        userDao.power(id);
        return "userindex";
    }

    @RequestMapping("/power1")
    public String getHtml2233(String id) {
        userDao.power1(id);
        return "userindex";
    }

    @RequestMapping("/lock")
    public String getHtml3(String id) {
        userDao.lock(id);
        return "userindex";
    }

    @RequestMapping("/unlock")
    public String getHtml4(String id) {
        userDao.unlock(id);
        return "userindex";
    }


   @RequestMapping("/login123")
    public int  loginweb(@RequestParam("username") String username,
                            @RequestParam("password") String password){
        List<Master> list = masterDao.find();
        for(int g=0;g<list.size();g++){
            Master us = list.get(g);
            if(username.equals(us.getAccount())){
                if(password.equals(us.getPassword())){

                    return 1;
                }
            }

        }
        return 0;
    }

    @RequestMapping("/login1234")
    public String login1234(String ticket,String randstr,String username,String password,HttpServletRequest request) {
        int z=loginweb(username,password);
        if(z==1){
            String userId=getIpAddr(request);
            int i=verifyTicket(ticket,randstr,userId);
            return "index";
        }
        return "404";
    }

    private static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static int verifyTicket(String ticket, String rand, String userIp) {

         final String APP_ID = "2082503859";
         final String APP_SECRET = "0wGH3gqUkO12AbcbuMMGDLg**";
         final String VERIFY_URI = "https://ssl.captcha.qq.com/ticket/verify?aid=%s&AppSecretKey=%s&Ticket=%s&Randstr=%s&UserIP=%s";

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet;

        CloseableHttpResponse response = null;
        try {
            httpGet = new HttpGet(String.format(VERIFY_URI,
                    APP_ID,
                    APP_SECRET,
                    URLEncoder.encode(ticket, "UTF-8"),
                    URLEncoder.encode(rand, "UTF-8"),
                    URLEncoder.encode(userIp, "UTF-8")
            ));
            response = httpclient.execute(httpGet);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String res = EntityUtils.toString(entity);
                System.out.println(res); // 临时输出

                JSONObject result = JSON.parseObject(res);
                // 返回码
                int code = result.getInteger("response");
                // 恶意等级
                int evilLevel = result.getInteger("evil_level");

                // 验证成功
                if (code == 1) return evilLevel;
            }
        } catch (java.io.IOException e) {
            // 忽略
        } finally {
            try {
                response.close();
            } catch (Exception ignore) {
            }
        }

        return -1;
    }
}
